package files;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;

import polarity.Polarity;

import classifier.Classifier;
import classifier.ClassifierManager;

import UI.JFrame;

import tools.Normalizer;

public class Tweet 
{
	private static Integer count = 0;
	private String type; // polarite
	private String mark;
	private String content;
	
	private Classifier classifierMark;
	
	private TreeMap<String, Integer> vocabulary;
	
	public Integer getId() { return count; }
	public String getType() { return type; }
	public String getMark() { return mark; }
	public String getContent() { return content; }
	public TreeMap<String, Integer> getVocabulary() { return vocabulary; }
	
	public float baynesForPositive;
	public float baynesForNegative;
	public float baynesForNeutral;
	public float baynesForIrrevelant;
	
	private static Hashtable<String,Float> bayesHt = new Hashtable<String,Float>();
	
	public ArrayList<Float> alBaynes = new ArrayList<Float>();
	
//	prior
//	aPriori
//	vraisemblance
//	test
	
	//prior = aPriori * vraisemblance / test;
	
	public void setClassifierMark(Classifier classifier)
	{
		classifierMark = classifier;
	}
	
	public Tweet(String type, String mark, String content, Normalizer normalizer)
	{
		this.count = count;
		count++;
		this.type = type;
		this.mark = mark;
		this.content = content;
		vocabulary = new TreeMap<String, Integer>();
		
		bayesHt.put("positive", baynesForPositive);
		bayesHt.put("negative", baynesForPositive);
		bayesHt.put("neutral", baynesForPositive);
		bayesHt.put("irrelevant", baynesForPositive);
		
		 try 
		 { 
			String newContent = "";
			if ( JFrame.optStopWordIsOn() == true)
		 	{
				
				for (String word : normalizer.normalize(content)) 
				 {
			 
				 	TreeMap<String, Integer> stopWord = ClassifierManager.getStopWord();
				 	word = word.toLowerCase();
			 		if ( stopWord.containsKey(word) == false )
			 		{
			 			addWord(word);
			 			newContent += word+" ";
			 		}
				 }
		 	}
			else
			{
				 for (String word : normalizer.normalize(content)) 
				 {		 	
					 word = word.toLowerCase();
					 addWord(word);
					 newContent += word+" ";
			    }
			}
			this.content = newContent;
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		
		
	}

	private void addWord(String word)
	{
		 if (vocabulary.containsKey(word)) 
         {
            
         	int count = vocabulary.get(word);
         	count++;
             vocabulary.put(word, count);
             
         } 
         else 
         {    	
             vocabulary.put(word, 1);
         }
	}
	
	public Integer getNumberOfWords()
	{
		int numberOfWords = 0;
		for (Map.Entry<String, Integer> set : vocabulary.entrySet() ) 
		{
			numberOfWords += set.getValue();
		}
		return numberOfWords;
	}
	
	public int getTweetSize()
        {
            return content.length();
        }
	
	public Integer getNumberOfDifferentWords()
	{
		return vocabulary.size();
	}
	
	public void choiceResultat()
	{
		float bayesMax = 0;
		String typeName = "irrelevant";
		
		for(Entry<String, Float> entryBayes : bayesHt.entrySet() )
		{
			if ( bayesMax < entryBayes.getValue() )
			{
				bayesMax = entryBayes.getValue();
				typeName = entryBayes.getKey();
				
			}
		}
		type = typeName;
	}
	
	public void calculateBayesMultinomial()
	{
		/*
		alBaynes.add(baynesForPositive);
		alBaynes.add(baynesForNegative);
		alBaynes.add(baynesForNeutral);
		alBaynes.add(baynesForIrrevelant);
		*/
		String[] words = content.split(" ");
		/*
		ArrayList<TreeMap<String, Float>> alBaynesClassifier = classifierMark.getBayesAl();
		for(int i=0; i<alBaynes.size();i++)
		{
			Float baynesTmp = alBaynes.get(i);
			TreeMap<String, Float> baynesClassifierTmp = alBaynesClassifier.get(i);
			baynesTmp = (float) (classifierMark.getNumberOfTweet()/ClassifierManager.getClassifierGeneral().getNumberOfTweet());
			System.out.println("lol"+classifierMark.getNumberOfTweet());
			System.out.println("alol"+ClassifierManager.getClassifierGeneral().getNumberOfTweet());
			for(int j=0;j<words.length;j++)
			{
				baynesTmp *= baynesClassifierTmp.get(words[j]);
			}
		}
		*/
		
		//HashTable<TreeMap<String, Float>> htBaynesClassifier = classifierMark.getPolarityManager().
		Hashtable<String, Polarity> polarityHt = classifierMark.getPolarityManager().getPolarityHt();
		// Pour chaque Polarite
		for(Entry<String, Polarity> entryPolarity : polarityHt.entrySet() )
		{
			Float baynes = bayesHt.get(entryPolarity.getKey());
			TreeMap<String, Float> baynesPolarity = entryPolarity.getValue().getBayes();
			baynes = (float) entryPolarity.getValue().getNumberOfTweet()*1000000000 / classifierMark.getPolarityManager().getNumberOfTweet();
			for(int i=0;i<words.length;i++)
			{
				if ( baynesPolarity.containsKey(words[i]) )
				{
					baynes *= baynesPolarity.get(words[i]);
				
					if ( baynes == 0f)
					{
						//System.out.println("bug");
//						System.out.println("Erreur 0 ::");
//						System.out.println(baynesPolarity.get(words[i]));
						//System.out.println("NbrMots - "+classifierMark.getPolarityManager().getPolarityHt().get(entryPolarity.getKey()).getNumberOfWords());
						//System.out.println("NbrMotsDif - "+classifierMark.getPolarityManager().getNumberOfDifferentWords());
					}
				}
				else // Mot qui n'a pas ete vu durant le training
				{
					baynes *= (float) 1/(classifierMark.getPolarityManager().getPolarityHt().get(entryPolarity.getKey()).getNumberOfWords()+classifierMark.getPolarityManager().getNumberOfDifferentWords());
					if ( baynes == 0f)
					{
						//System.out.println("IMPOSSIBLE");
					}
				}
				
			}
			
			bayesHt.put(entryPolarity.getKey(), baynes);
			//System.out.println(entryPolarity.getKey()+" : "+baynes);
		}
		choiceResultat();
		/*
		for(Entry<String, Float> entryBayes : bayesHt.entrySet() )
		{
			Float baynesTmp = entryBayes.getValue();
			
			TreeMap<String, Float> baynesClassifierTmp = alBaynesClassifier.get(i);
			baynesTmp = (float) (classifierMark.getNumberOfTweet()/ClassifierManager.getClassifierGeneral().getNumberOfTweet());
			System.out.println("lol"+classifierMark.getNumberOfTweet());
			System.out.println("alol"+ClassifierManager.getClassifierGeneral().getNumberOfTweet());
			for(int j=0;j<words.length;j++)
			{
				baynesTmp *= baynesClassifierTmp.get(words[j]);
			}
			
			
			
			System.out.println(entryBayes.getKey()+" : "+entryBayes.getValue());
		}
		*/
		/*
		for(int i=0;i<words.length;i++)
		{
			//System.out.println(words[i]);
			TreeMap<String, Integer> tm = ClassifierManager.getClassifierMicrosoft().getVocabularyPositiveWordCount();
			if ( tm.containsKey(words[i]) )
			{
				System.out.println(" Word : "+words[i]+" value "+(tm.get(words[i])+1));
			}
			else System.out.println(" Word : "+words[i]+" value 1");
			
			
			for(int j=0;j<alBaynes.size();j++)
			{
				
			}
			*/
			/*
			baynesForPositive;
			baynesForNegative;
			public float baynesForNeutral;
			public float baynesForIrrevelant;
			*/
		/*
		}
	*/
		/*
		System.out.print(ClassifierManager.getClassifierMicrosoft().getVocabularyPositive().size());
		System.out.print(" / ");
		System.out.print(ClassifierManager.getClassifierGeneral().getVocabularyPositive().size());
		System.out.println();
		*/
		
	}
	
}
